<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MTerm extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	function list_term($str = 'category') {
		$this->db->start_cache();
		$this->db->select(
				'term_id,term_name,term_slug,term_type,term_description,term_parent,term_count');
		$this->db->where('term_type', $str);

		if($this->db->get('terms')->num_rows() > 0) {
			$result = $this->db->get('terms')->result();
			$this->db->flush_cache();

			return $result;
		}
		return 0;
	}
	function get_list($term_id, $limit, $offset) {
		//$this->load->helper('typography');
		$this->db->where('term_id', $term_id);
		$this->db->select(
				'pid,post_author,post_title,post_content,post_name,post_modified,post_type,comment_count,post_veiws');
		$this->db->order_by('post_modified desc');
		$this->db->from('posts');
		$this->db->join('term_relationships',
				'term_relationships.post_id = posts.pid');
		$this->db->limit($limit, $offset);
		$result = $this->db->get()->result();
		//foreach ($result as $row){
		//$row->post_excerpt = auto_typography($row->post_excerpt, TRUE);
		//$row->post_excerpt = auto_typography($row->post_content, TRUE);
		//}
		return $result;
	}
	function count_all($term_id) {
		$this->db->where('term_id', $term_id);
		return $this->db->count_all_results('term_relationships');
	}
	function get_name($term_id) {
		$this->db->start_cache();
		$this->db->where('term_id', $term_id);
		$this->db->select(
				'term_id,term_name,term_slug,term_description,term_parent');

		$this->db->flush_cache();

		return $this->db->get('myh_terms')->row();
	}
	function  get_category_link($pid){
		$this->db->start_cache();
		$this->db->where('post_id',$pid);
		//$this->db->where('term_type','category');
		$this->db->select('terms.term_id,term_name,term_slug,term_type,term_description,term_parent,term_count');
		$this->db->from('terms');
		$this->db->join('term_relationships', 'term_relationships.term_id = terms.term_id','left');
		$result = $this->db->get()->row();

		$this->db->flush_cache();
		return $result;
	}
	function  get_category_id($pid){
		$this->db->start_cache();
		$this->db->where('post_id',$pid);
		//$this->db->where('term_type','category');
		$this->db->select('term_id');
		$this->db->from('term_relationships');
		$result = $this->db->get()->row();

		$this->db->flush_cache();
		return $result;
	}

	function update_veiws($pid,$str='post_veiws') {
		$sql = "UPDATE myh_posts SET ".$str." = ".$str." + 1 WHERE pid = ?";
		$this->db->query($sql, array($pid));

	}

	function insert() {
		$post['term_name'] = $this->input->post('term_name');
		$post['term_slug'] = str_replace(array(' ', '_','.','~','+'), '-', strtolower(trim($this->input->post('term_slug'))));
		$post['term_description'] = $this->input->post('term_description');
		$post['term_type'] = $this->input->post('term_type');
		$post['term_parent'] = $this->input->post('term_parent');
		$this->db->insert('terms', $post);

		return $this->db->insert_id();
	}

	function insert_relationships($pid) {
		$post['term_id'] = $this->input->post('term_id');
		$post['post_id'] = $pid;
		$this->db->insert('term_relationships', $post);
	}

}
/* End of file category.php */
/* Location: ./application/models/category.php */